/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import helper.NewHibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Added;
import pojo.CuahangProduct;
import pojo.CuahangProductId;
import pojo.Personnel;
import pojo.Product;

/**
 *
 * @author QTuan
 */
public class DAOCuaHangProduct {
    private Session session;
    public DAOCuaHangProduct()
    {
        SessionFactory factory = NewHibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    public CuahangProduct getCuaHangProductById(int idCuaHang,int idProduct)
    {
         try{
            session.getTransaction().begin();
             CuahangProductId cuahangproductid = new CuahangProductId(idCuaHang, idProduct);
            //String sql = "from CuahangProduct where id_cuahang = ? and id_product = ?";
           // Query query = session.createQuery(sql);
           // query.setInteger(0, idCuaHang);
           // query.setInteger(0, idProduct);
             CuahangProduct result = (CuahangProduct)session.get(CuahangProduct.class, cuahangproductid);
            
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public CuahangProduct getAndUpdate(CuahangProductId cuahangproID,Integer count)
    {
         try{
            session.getTransaction().begin();
            //String sql = "from CuahangProduct where id_cuahang = ? and id_product = ?";
           // Query query = session.createQuery(sql);
           // query.setInteger(0, idCuaHang);
           // query.setInteger(0, idProduct);
             CuahangProduct result = (CuahangProduct)session.get(CuahangProduct.class, cuahangproID);
            result.setCount(result.getCount() - count);
            session.update(result);
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public List<CuahangProduct> getCuaHangProduct(int idCuaHang)
    {
         try{
            session.getTransaction().begin();
            String sql = "from CuahangProduct where CuahangProduct.CuahangProductId.id_cuahang = ?";
            Query query = session.createSQLQuery("CALL smartshop.StoreProduct_GetStore(:vID)")
                    .addEntity(CuahangProduct.class).setParameter("vID",idCuaHang);
            //query.setInteger(0, idCuaHang);
            List<CuahangProduct> result = (List<CuahangProduct>)query.list();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public boolean updateProduct(CuahangProduct cuahangproduct)
    {
         try{
            session.getTransaction().begin();
            session.update(cuahangproduct);
            session.getTransaction().commit();
            return true;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
}
